package graphroute

import (
	"path/filepath"
	"power/core/dao/graphdao"
	"power/core/localwrap"
	"power/core/service/graph"
	"power/core/service/graphs"
	"power/http/routes"

	"github.com/RocksonZeta/irisx"
	"github.com/kataras/iris/v12"
)

var logRouteGraphElementProtos = localwrap.GetLogger().Fork("power/http/graphroute", "routeGraphElementProtos")

//Route /api/graph/element/proto
func routeGraphElementProtos(party iris.Party) {
	party.Get("/", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		classId := c.CheckQuery("classId").NotEmpty().Int(0)
		routes.Check(c)
		uid := c.GetUidInt()
		logRouteGraphElementProtos.Debug().Str("path", c.Path()).Int("classId", classId).Int("uid", uid).Send()
		r := graphs.GraphElementProtos{}.ListByClassId(classId)
		c.Ok(r)
	})
	party.Get("/{id:int}", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		id, _ := c.Params().GetInt("id")
		uid := c.GetUidInt()
		logRouteGraphElementProtos.Debug().Str("path", c.Path()).Int("id", id).Int("uid", uid).Send()
		r := graphs.GraphElementProtos{}.Get(id)
		c.Ok(r)
	})
	party.Get("/{id:int}/props", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		id, _ := c.Params().GetInt("id")
		uid := c.GetUidInt()
		logRouteGraphElementProtos.Debug().Str("path", c.Path()).Int("id", id).Int("uid", uid).Send()
		r := graphs.GraphElementProtos{}.GetWithProps(id)
		c.Ok(r)
	})
	party.Post("/", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param graphdao.GraphElementProtosAddParam
		param.Uid = c.GetUidInt()
		param.Name = c.CheckBody("name").NotEmpty().String()
		param.Type = c.CheckBody("type").NotEmpty().Int(0)
		param.ClassId = c.CheckBody("classId").NotEmpty().Int(0)
		routes.Check(c)
		file, header, err := c.FormFile("icon")
		if err != nil {
			logRouteGraphElementProtos.Error().Err(err).Stack().Msg(err.Error())
			c.AddParamError("icon", err.Error())
		}
		rpath, err := graphs.GraphElementProtos{}.SaveFile(file, param.Uid, filepath.Ext(header.Filename))
		if err != nil {
			logRouteGraphElementProtos.Error().Err(err).Stack().Msg(err.Error())
			c.AddParamError("icon", err.Error())
		}
		param.Icon = filepath.Join("/", rpath)
		props := graph.NewProps()
		props.SetName(param.Name)
		if param.Type == graph.ElementTypeEdge {
			props.SetStyle("arrow.to", false)
			props.SetStyle("edge.color", "#000")
			props.SetStyle("edge.width", 2)
		} else if param.Type == graph.ElementTypeText {
			props.SetStyle("label.border", 0.5)
			props.SetStyle("label.border.style", "#000")
			props.SetStyle("label.padding", 5)
			props.SetStyle("label.align.position", "lt") //left top
			props.SetName("")
		} else {
			props["image"] = param.Icon
		}
		param.Props = props.Marshal()
		routes.Check(c)
		r := graphs.GraphElementProtos{}.Add(param)
		c.Ok(r)
	})
	party.Post("/update", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		var param graphdao.GraphElementProtosUpdateParam
		param.Id = c.CheckBody("id").NotEmpty().Int(0)
		param.Name = c.CheckBody("name").NotEmpty().String()
		routes.Check(c)
		uid := c.GetUidInt()
		logRouteGraphElementProtos.Debug().Str("path", c.Path()).Interface("param", param).Int("uid", uid).Send()
		graphs.GraphElementProtos{}.Update(param)
		c.Ok(nil)
	})
	party.Post("/delete", func(ctx iris.Context) {
		c := ctx.(*irisx.Context)
		id := c.CheckBody("id").NotEmpty().Int(0)
		uid := c.GetUidInt()
		routes.Check(c)
		logRouteGraphElementProtos.Debug().Str("path", c.Path()).Int("id", id).Int("uid", uid).Send()
		graphs.GraphElementProtos{}.Delete(id)
		c.Ok(nil)
	})
}
